package cn.lym.zxxx.redis;

import cn.lym.zxxx.util.SerializeUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SimpleRedisClient implements RedisClient {

    private JedisPool jedisPool;

    public SimpleRedisClient(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public String set(byte[] key, byte[] value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key, value);
        }
    }

    public byte[] get(byte[] key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        }
    }

    public Long del(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(key);
        }
    }

    public Long del(byte[] key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(key);
        }
    }

    public Boolean exists(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.exists(key);
        }
    }

    public Boolean exists(byte[] key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.exists(key);
        }
    }

    public Boolean hexists(byte[] key, byte[] field) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hexists(key, field);
        }
    }

    public Long hset(String key, String field, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hset(key, field, value);
        }
    }

    public Long hset(byte[] key, byte[] field, byte[] value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hset(key, field, value);
        }
    }

    public Long hset(String key, String field, String value, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hset(key, field, value);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return (Long) result.get(0);
        }
    }

    public Long hset(byte[] key, byte[] field, byte[] value, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hset(key, field, value);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return (Long) result.get(0);
        }
    }

    public Object hget(byte[] key, byte[] field) {
        try (Jedis jedis = jedisPool.getResource()) {
            byte[] data = jedis.hget(key, field);
            return SerializeUtil.deserializeObject(data);
        }
    }

    public String hget(String key, String field) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hget(key, field);
        }
    }

    public String hget(String key, String field, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hget(key, field);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return (String) result.get(0);
        }
    }

    public Object hget(byte[] key, byte[] field, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hget(key, field);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return SerializeUtil.deserializeObject((byte[]) result.get(0));
        }
    }

    public String hmset(String key, Map<String, String> map) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hmset(key, map);
        }
    }

    public String hmset(byte[] key, Map<byte[], byte[]> map) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hmset(key, map);
        }
    }


    public String hmset(String key, Map<String, String> map, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hmset(key, map);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return (String) result.get(0);
        }
    }

    public String hmset(byte[] key, Map<byte[], byte[]> map, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hmset(key, map);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            return (String) result.get(0);
        }
    }

    public List<Object> hmget(byte[] key, int expire, byte[]... fields) {
        try (Jedis jedis = jedisPool.getResource()) {
            Transaction transaction = jedis.multi();
            transaction.hmget(key, fields);
            transaction.expire(key, expire);
            List<Object> result = transaction.exec();
            if (result == null || result.size() <= 0) {
                return null;
            }
            @SuppressWarnings("unchecked")
            List<byte[]> dataResult = (List<byte[]>) result.get(0);
            List<Object> list = new ArrayList<>();
            for (byte[] binaryData : dataResult) {
                list.add(SerializeUtil.deserializeObject(binaryData));
            }
            return list;
        }
    }

    public Long hdel(String key, String... field) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hdel(key, field);
        }
    }

    public Long hdel(byte[] key, byte[]... field) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hdel(key, field);
        }
    }
}
